package com.wangjie.arraylist;

import java.util.ArrayList;

public class ArrayListDemo {
    private static  MyCommparator myCommparator = new MyCommparator();
    private static  MyCommparator2 myCommparator2 = new MyCommparator2();

    public static <T> void main(String[] args) {
        ArrayList<T> arrayList = new ArrayList<T>();
        arrayList.add((T) "hello");
        arrayList.add((T) new Person("王杰",18));
        System.out.println(arrayList);

        ArrayList<String> array = new ArrayList<>();
        /*add*/
        boolean add =  array.add("hello");
        System.out.println(add);
        boolean add2 = array.add("world");
        System.out.println(add2);
        /*get*/
        String get = array.get(0);
        System.out.println(get);
        /*根据元素删除*/
        boolean remove =  array.remove("world");
        System.out.println(remove);
        System.out.println(array);
        /*在指定位置 添加元素*/
        array.add(1,"lalala");
        System.out.println(array);
        array.add("wangjie");
        System.out.println(array);
        /*添加集合 默认在末尾添加*/
        ArrayList<String> array2 = new ArrayList<>();
        array2.add("kobo");array2.add("durant");
        array.addAll(array2);
        System.out.println(array);
        /*在指定的位置添加集合*/
        array.addAll(1,array2);
        System.out.println(array);
        /*克隆一份集合*/
        Object obj = array.clone();
        System.out.println(obj);
        array.ensureCapacity(10);
        array.addAll(array2);
        array.addAll(array2);
        array.addAll(array2);
        array.addAll(array2);
        System.out.println(array);
        array.forEach(x -> System.out.print(x + "\t"));
        System.out.println("===============");
        /*按照字符串长度排序*/
        array.sort(myCommparator);
        System.out.println(array);
        /*按照首字母排序*/
        array.sort(myCommparator2);
        System.out.println(array);
    }
}
